<template>
  <div>
    <!-- 面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 搜索框 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryList.query"
            class="input-with-select"
            clearable
            @clear="clearQuery"
          >
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList()"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格数据 -->
      <el-table :data="goodsList" style="width: 100%" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格" width="80px"></el-table-column>
        <el-table-column prop="goods_number" label="商品数量" width="80px"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="80px"></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="150px">
          <template slot-scope="scope">{{scope.row.add_time | dateFormet}}</template>
        </el-table-column>
        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editGoods(scope.row.goods_id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteGoods(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 页码 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryList.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryList.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
      <!-- 编辑商品文本框 -->
      <el-dialog title="编辑商品" :visible.sync="editDialogVisible" width="30%" @close="closeEditGoods">
        <!-- 表单区域 -->
        <el-form ref="editFormRef" :model="editFormList" label-width="80px" :rules="editFormRules">
          <el-form-item label="商品名称" prop="goods_name">
            <el-input v-model="editFormList.goods_name"></el-input>
          </el-form-item>
          <el-form-item label="价格" prop="goods_price">
            <el-input v-model="editFormList.goods_price"></el-input>
          </el-form-item>
          <el-form-item label="数量" prop="goods_number">
            <el-input v-model="editFormList.goods_number"></el-input>
          </el-form-item>
          <el-form-item label="重量" prop="goods_weight">
            <el-input v-model="editFormList.goods_weight"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editDialog">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 请求数据参数
      queryList: {
        query: "",
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      // 商品数据列表
      goodsList: [],
      editDialogVisible: false,
      // 根据id获取的商品数据
      editFormList: [],
      editFormRules: {
        goods_name: [
          { required: true, message: "请输入商品名称", trigger: "blur" }
        ],
        goods_price: [
          { required: true, message: "请输入商品价格", trigger: "blur" }
        ],
        goods_number: [
          { required: true, message: "请输入商品数量", trigger: "blur" }
        ],
        goods_weight: [
          { required: true, message: "请输入商品重量", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    // 获取数据
    async getGoodsList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryList
      });
      if (res.meta.status !== 200) return this.$message.error("获取商品失败");
      this.goodsList = res.data.goods;
      this.total = res.data.total;
      console.log(this.goodsList);
    },
    // 一页显示多少条数据改变时触发函数
    handleSizeChange(newsize) {
      this.queryList.pagesize = newsize;
      this.getGoodsList();
    },
    // 改变页码时触发函数
    handleCurrentChange(newpage) {
      this.queryList.pagenum = newpage;
      this.getGoodsList();
    },
    // 清空input时输入框事件
    clearQuery() {
      this.queryList.query = "";
      this.getGoodsList();
    },
    // 点击添加商品跳转到添加商品页面
    addGoods() {
      this.$router.push("/goods/add");
    },
    // 编辑商品
    async editGoods(id) {
      const { data: res } = await this.$http.get(`goods/${id}`);
      if (res.meta.status !== 200) {
        return this.$message.error("获取失败");
      }
      this.editFormList = res.data;
      this.editDialogVisible = true;
      console.log(this.editFormList);
      console.log(typeof this.editFormList.goods_price);
    },
    // 点击确定发起更新请求
    editDialog() {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return;
        const { data: res } = await this.$http.put(
          `goods/${this.editFormList.goods_id}`,
          {
            goods_name: this.editFormList.goods_name,
            goods_cat: this.editFormList.goods_cat,
            goods_price: this.editFormList.goods_price,
            goods_number: this.editFormList.goods_number,
            goods_weight: this.editFormList.goods_weight
          }
        );
        if (res.meta.status !== 200) return this.$message.error("更新失败");
        this.getGoodsList();
        this.editDialogVisible = false;
      });
    },

    // 关闭对话框
    closeEditGoods() {
      this.$refs.editFormRef.resetFields();
    },
    // 根据id删除商品
    async deleteGoods(id) {
      const a = await this.$confirm(
        "此操作将永久删除该商品, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);
      if (!a === "confirm") return;
      const { data: res } = await this.$http.delete(`goods/${id}`);
      if (res.meta.status !== 200) return this.$message.error("删除失败");
      this.$message.success("删除成功");
      this.getGoodsList();
    }
  }
};
</script>

 <style lang="less" scoped>
</style>
